﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Pai.LiveStation.Data;
using Microsoft.Security.Application;
using System.Web.Security;

namespace Pai.LiveStation.Web.Controllers
{
    public class AdvertisementsController : Controller
    {
        //
        // GET: /Advertisements/

        public ActionResult Index()
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                ViewData["Advertisements"] = dc.Medias.Where(x => x.MediaType.Name.Equals(MediaType.Advertisement.ToString()));
            }

            return View();
        }

        public ActionResult Add()
        {

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(string adName, string adDesc, string adTags)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                Media media = new Media();
                media.Name = AntiXss.HtmlEncode(adName);
                media.Description = AntiXss.HtmlEncode(adDesc);
                media.MediaType = dc.MediaTypes.Where(x => x.Name.Equals(MediaType.Advertisement.ToString())).FirstOrDefault();
                Author author = CommonHelper.GetOrCreateAuthorForUser(Membership.GetUser());

                dc.Medias.InsertOnSubmit(media);
                List<string> tags = new List<string>();
                tags.AddRange(adTags.Split(';'));
                tags.ForEach(x =>
                {
                    string tagName = x;
                    Tag tag = dc.Tags.Where(y => y.ID.Equals(tagName)).FirstOrDefault();
                    if (tag == null)
                    {
                        tag.ID = tagName;
                        dc.Tags.InsertOnSubmit(tag);
                    }
                    media.MediaTags.Add(new MediaTag() { Media = media, Tag = tag});
                });
                    
                string uploadedFile = MediaHelper.UploadFile(Request.Files["adFile"]);

                if (String.IsNullOrEmpty(uploadedFile))
                    ModelState.AddModelError("adFile", "There was a problem uploading your file. Please contact your Executive Producers.");
                else
                    media.Location = uploadedFile;

                if (ModelState.IsValid)
                    dc.SubmitChanges();
                else
                    return View();

                return RedirectToAction("Index", "Advertisements");
            }
        }
        public ActionResult Modify(string id)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                int mediaID;

                if (!Int32.TryParse(id, out mediaID))
                {
                    ModelState.AddModelError("Error", "Invalid advertisement id");
                    return View();
                }

                ViewData["Advertisement"] = dc.Medias.Where(x=>x.ID == mediaID)
                    .Where(y=>y.MediaType.Equals(MediaType.Advertisement.ToString())).FirstOrDefault();

                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Modify(string id, string adName, string adDesc, string adTags)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                int mediaID;
                Media media = new Media();

                if (!Int32.TryParse(id, out mediaID))
                {
                    ModelState.AddModelError("Error", "Invalid advertisement id");
                    return View();
                }
                media = dc.Medias.Where(x => x.ID == mediaID).Where(y => y.MediaType.Equals(MediaType.Advertisement.ToString())).FirstOrDefault();

                if (media == null)
                {
                    ModelState.AddModelError("Error", "Invalid advertisement");
                    return View();
                }
                
                media.Name = AntiXss.HtmlEncode(adName);
                media.Description = AntiXss.HtmlEncode(adDesc);

                List<string> tags = new List<string>();
                tags.AddRange(adTags.Split(';'));
                tags.ForEach(x =>
                {
                    string tagName = x;
                    Tag tag = dc.Tags.Where(y => y.ID.Equals(tagName)).FirstOrDefault();
                    if (tag == null)
                    {
                        tag.ID = tagName;
                        dc.Tags.InsertOnSubmit(tag);
                    }
                    media.MediaTags.Add(new MediaTag() { Media = media, Tag = tag });
                });

                if (ModelState.IsValid)
                    dc.SubmitChanges();


                ViewData["Advertisement"] = media;
                
                return View();
            }
        }
        public ActionResult Delete(string id)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                int mediaID;
                Media media = new Media();

                if (!Int32.TryParse(id, out mediaID))
                {
                    ModelState.AddModelError("Error", "Invalid advertisement id");
                    return RedirectToAction("Index");
                }

                media = dc.Medias.Where(x => x.ID == mediaID).FirstOrDefault();

                if (media == null)
                {
                    ModelState.AddModelError("Error", "Advertisement does not exist.");
                    return RedirectToAction("Index");
                }

                dc.Medias.DeleteOnSubmit(media);
                dc.SubmitChanges();

                return RedirectToAction("Index");
            }
        }
    }
}
